package org.example.stuview;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;

public class HandleTest implements ActionListener {
    int topicNumber = 1;
    TestView testView;
    Socket socket=null;
    DataInputStream in=null;
    DataOutputStream out=null;
    Thread thread;

    @Override
    public void actionPerformed(ActionEvent e) {
        InetAddress address= null;
        socket=new Socket();
        thread=new Thread(testView);
        try {
            address = InetAddress.getByName("127.0.0.1");
            InetSocketAddress socketAddress=new InetSocketAddress
                    (address,4331);
            socket.connect(socketAddress);
            in =new DataInputStream(socket.getInputStream());
            out = new DataOutputStream(socket.getOutputStream());
            thread.start();
        } catch (IOException ex) {
            socket=new Socket();
        }

        if (e.getSource() == testView.submit){  //提交试卷
            if (testView.isSelect.get(4).equals("未知") && TestView.TopicArea.getText().startsWith("第5题")){
                if (testView.Select_A.isSelected()){
                    testView.isSelect.set(4,"A");
                } else if (testView.Select_B.isSelected()){
                    testView.isSelect.set(4,"B");
                } else if (testView.Select_C.isSelected()){
                    testView.isSelect.set(4,"C");
                } else if (testView.Select_D.isSelected()){
                    testView.isSelect.set(4,"D");
                }
            }else {
                if (testView.Select_A.isSelected()){
                    testView.isSelect.set(topicNumber-1,"A");
                } else if (testView.Select_B.isSelected()){
                    testView.isSelect.set(topicNumber-1,"B");
                } else if (testView.Select_C.isSelected()){
                    testView.isSelect.set(topicNumber-1,"C");
                } else if (testView.Select_D.isSelected()){
                    testView.isSelect.set(topicNumber-1,"D");
                }
            }
            //向服务器发送答案
            String ans;        //发送学生ID
            ans = new String("学生ID"+testView.stuID+" 学生答案：");
            for (int i = 0; i < testView.isSelect.size(); i++) {
                if (testView.isSelect.get(i).equals("未知")){
                    ans += "E";
                }else ans += testView.isSelect.get(i);
            }
            ans += TestView.test_paper;
            System.out.println(ans);
            try {
                out.writeUTF(ans);
            } catch (IOException we) {
                throw new RuntimeException(we);
            }
            //打印答案
            for (int i = 0; i < testView.isSelect.size(); i++) {
                System.out.println(testView.isSelect.get(i));
            }
            //成绩界面
            ScoreView scoreView = new ScoreView();
            scoreView.stuID = testView.stuID;
            scoreView.stuName = testView.stuName;
            testView.setVisible(false);
            testView.timerTask.cancel();
            scoreView.init();
            scoreView.scoreListener();
        }else if (e.getSource() == testView.lastTopic){  //上一题
            if (topicNumber == 1){
            } else{
                topicNumber --;
                TestView.TopicArea.setText(TestView.topics.get(topicNumber-1));
                if (testView.Select_A.isSelected()){
                    testView.isSelect.set(topicNumber,"A");
                } else if (testView.Select_B.isSelected()){
                    testView.isSelect.set(topicNumber,"B");
                } else if (testView.Select_C.isSelected()){
                    testView.isSelect.set(topicNumber,"C");
                } else if (testView.Select_D.isSelected()){
                    testView.isSelect.set(topicNumber,"D");
                }
                testView.bg.clearSelection();
            }
        }else if (e.getSource() == testView.nextTopic){    //下一题
            if (topicNumber == 5){
            } else{
                topicNumber ++;
                TestView.TopicArea.setText(TestView.topics.get(topicNumber-1));
                if (testView.Select_A.isSelected()){
                    testView.isSelect.set(topicNumber-2,"A");
                } else if (testView.Select_B.isSelected()){
                    testView.isSelect.set(topicNumber-2,"B");
                } else if (testView.Select_C.isSelected()){
                    testView.isSelect.set(topicNumber-2,"C");
                } else if (testView.Select_D.isSelected()){
                    testView.isSelect.set(topicNumber-2,"D");
                }
                testView.bg.clearSelection();
            }
        }

    }

    public void setTestView(TestView testView) {
        this.testView = testView;
    }
}
